package com.vinoxm.common.utils;

public class StringUtils {

    public static boolean isEmpty(String str) {
        return null == str || "".equals(str);
    }

    public static void ifEmptyThrow(String str, RuntimeException exception) throws RuntimeException{
        if (isEmpty(str)) throw exception;
    }

    public static String isOrEmpty(String str, String def) {
        return isEmpty(str) ? def : str;
    }

    public static boolean equals(String str1, String str2) {
        return str1 != null && str1.equals(str2);
    }

    // 转换成驼峰写法
    public static String humpWork(String inputString, boolean firstCap) {
        if (StringUtils.isEmpty(inputString)) {
            return "";
        }
        String result = humpWork(inputString);
        if (firstCap) {
            return StringUtils.uppercaseFirst(result);
        }
        return result;
    }

    public static String humpWorkGet(String inputString) {
        if (StringUtils.isEmpty(inputString)) {
            return "";
        }
        return "get" + uppercaseFirst(humpWork(inputString));
    }

    public static String humpWork(String inputString) {
        StringBuilder result = new StringBuilder();
        boolean nextUpper = false;
        for (int i = 0; i < inputString.length(); i++) {
            char s = inputString.charAt(i);
            if (StringUtils.equals(String.valueOf(s), "_")) {
                nextUpper = true;
            } else if (nextUpper) {
                result.append(Character.toUpperCase(s));
                nextUpper = false;
            } else {
                result.append(s);
            }
        }
        return result.toString();
    }

    public static String uppercaseFirst(String inputString) {
        if (StringUtils.isEmpty(inputString)) {
            return "";
        }
        char firstLetter = inputString.charAt(0);
        char capitalFirstLetter = Character.toUpperCase(firstLetter);
        char[] charArray = inputString.toCharArray();
        charArray[0] = capitalFirstLetter;
        return String.valueOf(charArray);
    }

    public static String lowercaseFirst(String inputString) {
        if (StringUtils.isEmpty(inputString)) {
            return "";
        }
        char firstLetter = inputString.charAt(0);
        char capitalFirstLetter = Character.toLowerCase(firstLetter);
        char[] charArray = inputString.toCharArray();
        charArray[0] = capitalFirstLetter;
        return String.valueOf(charArray);
    }
}
